﻿namespace Mp3Observer.Common.Language
{
    #region usings

    using System;
    using System.ComponentModel;
    using System.Globalization;
    using System.Threading;
    using System.Windows;
    using System.Windows.Data;

    #endregion

    /// <summary>
    /// This class stores the current cultureinfo of the Client and is used for the "dummy"-binding on the controls
    /// to support changing the language on the UI at runtime.
    /// </summary>
    public sealed class LanguageContext : Singleton<LanguageContext>, INotifyPropertyChanged
    {
        #region Fields

        /// <summary>
        /// the culture 
        /// </summary>
        private CultureInfo cultureInfo;

        #endregion

        #region Events

        /// <summary>
        /// Occurs when a property value changes.
        /// </summary>
        public event PropertyChangedEventHandler PropertyChanged;

        #endregion

        #region Properties

        /// <summary>
        /// Gets or sets the culture.
        /// </summary>
        /// <value>The culture.</value>
        /// <exception cref="ArgumentNullException">if value is null</exception>
        public CultureInfo Culture
        {
            get { return this.cultureInfo; }
            set
            {
                // value.ThrowIfNull("value");
                if (value == this.cultureInfo)
                    return;
                this.cultureInfo = value;
                Thread.CurrentThread.CurrentUICulture = this.cultureInfo;
                Thread.CurrentThread.CurrentCulture = this.cultureInfo;
                if (Application.Current != null)
                {
                    if (this.cultureInfo.TextInfo.IsRightToLeft)
                        Application.Current.MainWindow.FlowDirection = FlowDirection.RightToLeft;
                    else
                        Application.Current.MainWindow.FlowDirection = FlowDirection.LeftToRight;
                }
                this.OnPropertyChanged("Culture");
            }
        }

        #endregion

        #region Public Methods

        /// <summary>
        /// this mehtod binds the _target._property to the Culture DP
        /// </summary>
        /// <param name="converter">the language converter that really does the language convertion</param>
        /// <param name="target">The target.</param>
        /// <param name="property">The property.</param>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1822:MarkMembersAsStatic", Justification = "it is accessed by Singleton")]
        public void DoBinding(IValueConverter converter, DependencyObject target, DependencyProperty property)
        {
            Binding cultureBinding = new Binding("Culture");
            cultureBinding.Mode = BindingMode.TwoWay;
            cultureBinding.Source = LanguageContext.Instance;
            cultureBinding.Converter = converter;
            BindingOperations.SetBinding(target, property, cultureBinding);
        }

        #endregion

        #region Private Methods

        /// <summary>
        /// Called when [property changed].
        /// </summary>
        /// <param name="property">The property.</param>
        private void OnPropertyChanged(string property)
        {
            if (this.PropertyChanged != null)
                this.PropertyChanged(this, new PropertyChangedEventArgs(property));
        }

        #endregion
    }
}
